package com.mypro.common.user;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.mypro.common.base.BaseController;

/** 
 * @ClassName: UserController 
 * @Description: 用户操作控制器
 * @author liuxd
 * @date 2013-7-1 上午11:14:22 
 *  
 */
@RequestMapping(value="/user")
@Controller
public class UserController extends BaseController{
	
	private static Logger log = Logger.getLogger(UserController.class);

	@Autowired
	private UserService userService;
	/** 
	 * @Title: toLoginPage 
	 * @Description: 跳转到login页面
	 * @return
	 * @throws 
	 */
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String toLoginPage(){
		return "common/login";
	}
	
	@RequestMapping(value="/left",method=RequestMethod.GET)
	public String toLeftPage(){
		return "common/left";
	}
	
	@RequestMapping(value="/right",method=RequestMethod.GET)
	public String toRightPage(){
		return "common/right";
	}
	
	@RequestMapping(value="/top",method=RequestMethod.GET)
	public String toTopPage(){
		return "common/top";
	}
	
	@RequestMapping(value="/foot",method=RequestMethod.GET)
	public String toFootPage(){
		return "common/foot";
	}
	
	/** 
	 * @Title: Login 
	 * @Description: 用户登陆
	 * @param userName
	 * @param password
	 * @param request
	 * @return
	 * @throws 
	 */
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public String Login(String userName,String password,HttpServletRequest request){
		log.info("用户  "+userName +"  登陆  IP:"+request.getLocalAddr());
		List<User> list = userService.login(userName,password);
		if(list.size()>0){
			request.getSession().setAttribute("loginUser", list.get(0));
			return "common/main";
		}
		log.info("用户  "+userName+"  登陆失败");
		return "common/login";
	}
	
	/** 
	 * @Title: out 
	 * @Description: 用户注销
	 * @param request
	 * @return
	 * @throws 
	 */
	@RequestMapping(value="/out",method=RequestMethod.GET)
	public String out(HttpServletRequest request){
		User loginUser = (User)request.getSession().getAttribute("loginUser");
		request.getSession().removeAttribute("loginUser");
		log.info("用户  "+loginUser.getUsername() +"  注销成功");
		return "common/login";
	}
}
